<template>
  <div class="box">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/page' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/goods' }">商品列表</el-breadcrumb-item>
      <el-breadcrumb-item>添加商品</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <div class="head">
        <span><i class="el-icon-info"></i>添加商品信息</span>
      </div>
      <!-- 顶部 -->
      <el-steps
        :space="200"
        :active="active"
        finish-status="success"
        style="margin-top: 20px; margin-left: 120px"
      >
        <el-step title="基本信息"></el-step>
        <el-step title="商品参数"></el-step>
        <el-step title="商品属性"></el-step>
        <el-step title="商品图片"></el-step>
        <el-step title="商品内容"></el-step>
        <el-step title="商品完成"></el-step>
      </el-steps>
      <!-- 左侧tab -->
      <el-tabs
        :tab-position="tabPosition"
        :before-leave="before"
        style="margin-top: 20px"
        x
        v-model="tab"
        @tab-click="addtab"
      >
        <el-tab-pane label="基本信息" name="0">
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="商品名称" prop="goods_name">
              <el-input v-model="ruleForm.goods_name"></el-input>
            </el-form-item>
            <el-form-item label="商品价格" prop="goods_price">
              <el-input type="number" v-model="ruleForm.goods_price"></el-input>
            </el-form-item>
            <el-form-item label="商品重量" prop="goods_weight">
              <el-input
                type="number"
                v-model="ruleForm.goods_weight"
              ></el-input>
            </el-form-item>
            <el-form-item label="商品数量" prop="goods_number">
              <el-input
                type="number"
                v-model="ruleForm.goods_number"
              ></el-input>
            </el-form-item>
            <el-form-item label="商品分类">
              <div class="block">
                <span class="demonstration">选择商品分类</span>
                <!-- 级联菜单 -->
                <el-cascader
                  v-model="ruleForm.goods_cat"
                  :options="options"
                  :props="{
                    expandTrigger: 'hover',
                    label: 'cat_name',
                    value: 'cat_id',
                    children: 'children',
                  }"
                  @change="handleChange"
                ></el-cascader>
              </div>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="商品参数" name="1">
          <el-form v-model="ruleForm">
            <el-form-item
              :label="item.attr_name"
              v-for="(item, index) in manyTableData"
              :key="index"
            >
              <el-checkbox-group v-model="item.newValue">
                <el-checkbox
                  v-for="(ite, ind) in item.attr_vals"
                  :key="ind"
                  :label="ite"
                  border
                ></el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="商品属性" name="2">
          <el-form v-model="ruleForm">
            <el-form-item
              :label="item.attr_name"
              v-for="(item, index) in onlyTableData"
              :key="index"
            >
              <el-checkbox-group v-model="item.newValue">
                <el-checkbox
                  v-for="(ite, ind) in item.attr_vals"
                  :key="ind"
                  :label="ite"
                  border
                ></el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="商品图片" name="3">
          <el-upload
            class="upload-demo"
            action="http://timemeetyou.com:8889/api/private/v1/upload"
            :on-success="success"
            :on-preview="preview"
            list-type="picture"
            :headers="headers"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过500kb
            </div>
          </el-upload>
        </el-tab-pane>
        <el-tab-pane label="商品内容" name="4">
          <div>
            <quill-editor
              ref="myTextEditor"
              v-model="ruleForm.goods_introduce"
              :options="editorOption"
              style="height: 230px"
              @change="onEditorChange"
            ></quill-editor>
          </div>
          <el-button
            style="margin-top: 50px"
            type="primary"
            @click="addCart('ruleForm')"
            >添加商品</el-button
          >
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <!-- 图片预览 -->
    <el-dialog title="图片预览" :visible.sync="dialogTableVisible">
      <img style="width: 100%" :src="img" alt="" />
    </el-dialog>
    <!-- 图片预览 -->
  </div>
</template>

<script>
import {
  goods,
  categories,
  attributes,
  upload,
  goodsss,
  deletegoods,
} from '@/api/login.js'
// import 'quill/dist/quill.core.css'
// import 'quill/dist/quill.snow.css'
// import 'quill/dist/quill.bubble.css'
// import { quillEditor } from 'vue-quill-editor'
export default {

  data() {
    return {
      // 上传图片
      fileList: [],
      // 上传图片
      tab: '0',
      active: 0,
      tabPosition: 'left',
      ruleForm: {
        //商品名称
        goods_name: '',
        //商品价钱
        goods_price: 0,
        //商品重量
        goods_weight: 0,
        //商品数量
        goods_number: 0,
        //商品分类
        goods_cat: '',
        //商品描述
        goods_introduce: '',
        //商品图片
        pics: '',
      },
      editorOption: {
        placeholder: '编辑文章内容',
      },
      rules: {
        goods_name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' },
        ],
        goods_price: [
          { required: true, message: '请输入商品名称', trigger: 'blur' },
        ],
        goods_weight: [
          { required: true, message: '请输入商品重量', trigger: 'blur' },
        ],
        goods_number: [
          { required: true, message: '请输入商品数量', trigger: 'blur' },
        ],
      },
      value: [],
      //options是三级联动
      options: [],
      // 参数id
      attr_id: 0,
      headers: {
        Authorization: localStorage.getItem('token'),
      },
      dialogTableVisible: false,
      img: '',
      //动态
      manyTableData: [],
      //静态
      onlyTableData: [],
    }
  },
  mounted() {
    categories().then((res) => {
      // console.log(res.data.data, '0')
      this.options = res.data.data
    })
  },
  methods: {
    //商品分类级联菜单
    handleChange(option) {
      this.attr_id = option[option.length - 1]
      this.ruleForm.goods_cat = option.join(',')
    },
    addtab() {
      if (this.tab == 1) {
        // console.log('aaa')
        attributes(this.attr_id, 'many').then((res) => {
          console.log(res)
          // 分类参数
          if (res.data.meta.status == 200) {
            res.data.data.forEach((item) => {
              item.attr_vals = item.attr_vals.split(' ')
              item.newValue = [...item.attr_vals] //深拷贝一份,解决多选消失
            })
            this.$message.success(res.data.meta.msg)
          }
          this.manyTableData = res.data.data
          console.log(this.manyTableData)
        })
      }
      if (this.tab == 2) {
        attributes(this.attr_id, 'only').then((res) => {
          console.log(res)
          // 分类参数
          if (res.data.meta.status == 200) {
            this.$message.success(res.data.meta.msg)
            res.data.data.forEach((item) => {
              console.log(item)
              item.attr_vals = item.attr_vals.split(' ')
              item.newValue = [...item.attr_vals]
              //深拷贝一份,解决多选消失
            })
            this.onlyTableData = res.data.data
          }
        })
      }
    },
    before(activeName, oldActiveName) {
      if (oldActiveName === '0' && this.ruleForm.goods_cat == '') {
        this.$message.error('请选择商品分类')
        return false
      }
      this.active = Number(activeName)
    },
    success(res) {
      console.log(res)
      if (res.meta.status == 200) {
        this.ruleForm.pics = res.data.tmp_path
        this.$message.success(res.meta.msg)
      }
    },
    preview(res) {
      console.log(res)
      this.dialogTableVisible = true
      this.img = res.response.data.url
    },
    onEditorChange({ editor, html, text }) {
      this.ruleForm.goods_introduce = html
      // console.log(this.ruleForm.goods_introduce)
    },
    addCart(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          goodsss(this.ruleForm).then((res) => {
            // console.log(res)
            if (res.data.meta.status == 201) {
              this.$message.success(res.data.meta.msg)
              this.$router.push('/goods')
            }
          })
        } else {
          this.$message.error('请填写完整必要的表单性')
          return false
        }
      })
    },
  },
  computed: {},
  watch: {},
}
</script>

<style scoped lang="scss">
.box {
  padding: 20px;
  .box-card {
    margin-top: 20px;
    height: 500px;
    .head {
      width: 100%;
      height: 30px;
      font-size: 14px;
      background: rgb(236, 236, 236);
      padding: 0px 20px;
      text-align: center;
      line-height: 30px;
    }
  }
}
.demonstration {
  color: red;
  margin: 0px 20px;
}
.img {
  width: 100%;
  height: 100%;
  background: #000;
}
</style>
